package com.misty.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.misty.common.bean.Result;
import com.misty.common.constants.ResultEnum;
import com.misty.dao.domain.Type;
import com.misty.dao.mapper.TypeMapper;
import com.misty.admin.service.TypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cthulhu
 * @since 2022-11-21
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Resource
    private TypeMapper typeMapper;



    /**
     *
     * @Param
     * @return
     * @Description 新增类型
     * @Date
     **/
    @Override
    public Result<?> saveType(Type type) {
        type.setCreateTime(LocalDateTime.now());
        type.setUpdateTime(LocalDateTime.now());
        int insert = typeMapper.insert(type);
        if(insert <= 0){
            Result.error(ResultEnum.FAILURE.getValue(),"新增类型失败！");
        }
        return Result.success(ResultEnum.SUCCESS.getValue(),"新增类型成功！");
    }

    /**
     *
     * @Param
     * @return
     * @Description 更新类型
     * @Date
     **/
    @Override
    public Result<?> updateType(Type type) {
        type.setUpdateTime(LocalDateTime.now());
        int update = typeMapper.updateById(type);
        if(update <= 0){
            Result.error(ResultEnum.FAILURE.getValue(),"更新类型失败！");
        }
        return Result.success(ResultEnum.SUCCESS.getValue(),"更新类型成功！");
    }


    /**
     *
     * @Param
     * @return
     * @Description 删除类型
     * @Date
     **/
    @Override
    public Result<?> deleteType(Integer typeId) {
        int delete = typeMapper.deleteById(typeId);
        if(delete <= 0){
            Result.error(ResultEnum.FAILURE.getValue(),"删除类型失败！");
        }
        return Result.success(ResultEnum.SUCCESS.getValue(),"删除类型成功！");
    }

    /**
     *
     * @Param
     * @return
     * @Description 分页查询类型
     * @Date
     **/
    @Override
    public Result<?> Page(Integer pageNum, Integer pageSize, String search
    ) {
        LambdaQueryWrapper<Type> typeLambdaQueryWrapper = Wrappers.<Type>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            typeLambdaQueryWrapper.like(Type::getName, search);
        }
        Page<Type> typePage = typeMapper.selectPage(new Page<>(pageNum, pageSize),typeLambdaQueryWrapper);
        if(typePage.getTotal() <= 0){
            Result.error(ResultEnum.FAILURE.getValue(),"分页查询类型失败！");
        }
        return Result.success(ResultEnum.SUCCESS.getValue(),"分页查询类型成功！",typePage);
    }


}
